<?php
Class Comment extends CI_Controller{
		
		function post_comment(){
			$login_info = $this->session->userdata("login_info") ;
			if ($login_info){
				$comment = $this->input->post("comment");
				$this->load->model("md_common");
				$array= array(
					'comment' => $comment,
					'user_id' => $login_info->user_id
				);
				$comment_id = $this->md_common->insert("comment",$array);	
		
				echo json_encode(array('status'=>"success","comment_id"=>$comment_id, "comment"=>$comment,"user_name"=>$login_info->username,"user_id"=>$login_info->user_id));
			}else{
				redirect(base_url("/user/login"));
			}
			
		}
		function post_comment_detail(){
			$login_info = $this->session->userdata("login_info") ;
			if ($login_info){
				$comment = $this->input->post("comment");
				$comment_id = $this->input->post("comment_id");
				$this->load->model("md_common");
				$array= array(
					'comment_detail' => $comment,
					'user_id' => $login_info->user_id,
					'comment_id' =>$comment_id
				);
				$comment_detail_id = $this->md_common->insert("comment_detail",$array);	
		
				echo json_encode(array('status'=>"success","comment"=>$comment,"comment_detail_id"=>$comment_detail_id,"user_name"=>$login_info->username,"user_id"=>$login_info->user_id));
			}else{
				redirect(base_url("/user/login"));
			}
			
		}
		
		function get_all_comment(){
			
			
			$comments= $this->md_common->query("select  a.*,b.username,b.user_id from `comment` a inner join `user` b on  a.user_id = b.user_id");
			$arr_ret = array(); 
			
			foreach ($comments as $item){
				$arr_comm = array('comment' => $item);
				$comment_id = $item->id;
				//get comments detail
				$comment_detail = $this->md_common->query("select  a.*,b.username,b.user_id from `comment_detail` a inner join `user` b on  a.user_id = b.user_id where a.comment_id = $comment_id");
				$arr_comm['comment_detail'] = $comment_detail;
				
				array_push($arr_ret,$arr_comm);
			}
			return  $arr_ret;
		}
		function delete_comment(){
			$comment_id = $this->input->post("comment_id");
			$comment_id = addslashes($comment_id);
			
			$this->load->model("md_common");
			$comments= $this->md_common->delete("comment",array("id" => $comment_id));
		}
		function delete_comment_detail(){
			$comment_detail_id = $this->input->post("comment_detail_id");
			$comment_detail_id = addslashes($comment_detail_id);
			
			$this->load->model("md_common");
			$comments= $this->md_common->delete("comment_detail",array("comment_detail_id" => $comment_detail_id) );
		}
	} 
?>